package com.qf.fragment;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.qf.homeproject.R;

public class FindFragment extends Fragment
{
	private ImageView imageContent;
	private ImageView imageAnim;
	private TextView textView;
	private Handler handler = new Handler();
	AnimationDrawable anim;
	boolean isLoading = true;
	private static Bundle bundle;

	public static Fragment newInstance(String title)
	{
		FindFragment findFrag = new FindFragment();
		bundle = new Bundle();
		bundle.putString("title", title);
		findFrag.setArguments(bundle);
		return findFrag;
	}

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LoadDataFromServer();
	}

	private void initShow()
	{
		imageContent.setVisibility(View.INVISIBLE);
		textView.setVisibility(View.INVISIBLE);
	}

	private void LoadDataFromServer()
	{
		new Thread()
		{
			// 子线程
			public void run()
			{
				try
				{
					Thread.sleep(7000);
					isLoading = false;
					handler.post(new Runnable()
					{

						// 主线程
						@Override
						public void run()
						{
							if (!isLoading)
							{
								imageContent.setVisibility(View.VISIBLE);
								textView.setVisibility(View.VISIBLE);
								imageAnim.setVisibility(View.INVISIBLE);
							}
						}
					});
				}
				catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		}.start();
	}

	/**
	 * 初始化详情页面的动画
	 */
	private void initAnimation()
	{
		imageAnim.setBackgroundResource(R.anim.frame_anim);
		anim = (AnimationDrawable) imageAnim.getBackground();
		if (!anim.isRunning())
		{
			// 开始逐帧动画
			anim.start();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState)
	{
		View view = inflater.inflate(R.layout.fragment_find, null);
		textView = (TextView) view.findViewById(R.id.fragment_id_tv);
		imageContent = (ImageView) view.findViewById(R.id.tong_id);
		imageAnim = (ImageView) view.findViewById(R.id.animation_id);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState)
	{
		initShow();
		initAnimation();
		super.onActivityCreated(savedInstanceState);
	}
}
